package lacus.wd_cinema.component;

import lacus.wd_cinema.entity.User;
import lacus.wd_cinema.repository.UserRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class AdminInitializer implements CommandLineRunner {
    private final UserRepository userRepository;

    private static final String ADMIN_USERNAME = "admin";
    private static final String ADMIN_PASSWORD = "ynuinfo#777";
    private static final String ADMIN_PHONE_NUMBER = "123456789";

    public AdminInitializer(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    @Override
    public void run(String... args) {
        System.out.println("管理员密码："+ ADMIN_PASSWORD);
        // 若系统中没有管理员，添加初始管理员
        if (userRepository.findByUsername(ADMIN_USERNAME).isEmpty()) {
            User admin = new User();
            admin.setUsername(ADMIN_USERNAME);
            admin.setPassword(ADMIN_PASSWORD);
            admin.setPhoneNumber(ADMIN_PHONE_NUMBER);
            admin.setRole(User.Role.ROLE_ADMIN);
            userRepository.save(admin);
            log.info("已添加管理员admin");
        }
    }
}
